package com.ktwlsoft.archivemanage.controller.study;

import com.ktwlsoft.archivemanage.domain.business.businessService.study.StudyManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 课程管理
 *
 * @author Name on 2019/12/24 15:33
 * @version 1.0
 */
@Api(value = "课程管理", description = "课程管理")
@RestController
@RequestMapping("study")
public class StudyManageController {

    private final StudyManageService studyManageService;

    public StudyManageController(StudyManageService studyManageService) {
        this.studyManageService = studyManageService;
    }

    /**
     * 首页 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "首页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/home")
    public ResponseEntity<?> home(){
        return ResponseEntity.ok(studyManageService.home("MANAGE"));
    }

    /**
     * 用户端首页 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "用户端首页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "user/home")
    public ResponseEntity<?> userHome(){
        return ResponseEntity.ok(studyManageService.home("USER"));
    }

    /**
     * 搜索列表 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "搜索列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/search")
    public ResponseEntity<?> search(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "keyWords",value = "关键字") @RequestParam(defaultValue = "", required = false) String keyWords,
                                      @ApiParam(name = "classifyId",value = "分类id") @RequestParam(defaultValue = "", required = false) Long classifyId,
                                      @ApiParam(name = "sortField", value = "排序字段") @RequestParam(defaultValue = "", required = false) String sortField,
                                      @ApiParam(name = "sortType", value = "排序方式:descend(降序),ascend(升序)") @RequestParam(defaultValue = "", required = false)String sortType){
        return ResponseEntity.ok(studyManageService.search(pn,pageSize,keyWords,classifyId,sortField,sortType,"MANAGE"));
    }

    /**
     * 用户端搜索列表 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "用户端搜索列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "user/search")
    public ResponseEntity<?> userSearch(@RequestParam(defaultValue = "1") int pn,
                                    @RequestParam(defaultValue = "30") int pageSize,
                                    @ApiParam(name = "keyWords",value = "关键字") @RequestParam(defaultValue = "", required = false) String keyWords,
                                    @ApiParam(name = "classifyId",value = "分类id") @RequestParam(defaultValue = "", required = false) Long classifyId,
                                    @ApiParam(name = "sortField", value = "排序字段") @RequestParam(defaultValue = "", required = false) String sortField,
                                    @ApiParam(name = "sortType", value = "排序方式:descend(降序),ascend(升序)") @RequestParam(defaultValue = "", required = false)String sortType){
        return ResponseEntity.ok(studyManageService.search(pn,pageSize,keyWords,classifyId,sortField,sortType,"USER"));
    }

    /**
     * 热门课程 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "热门课程", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/moreHots")
    public ResponseEntity<?> moreHots(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize){
        return ResponseEntity.ok(studyManageService.moreHots(pn,pageSize,"MANAGE"));
    }

    /**
     * 用户端热门课程 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "用户端热门课程", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "user/moreHots")
    public ResponseEntity<?> userMoreHots(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize){
        return ResponseEntity.ok(studyManageService.moreHots(pn,pageSize,"USER"));
    }

    /**
     * 课程详情 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "课程详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/class")
    public ResponseEntity<?> getDetails(@ApiParam(name = "id", value = "课程id") @RequestParam Long id){
        return ResponseEntity.ok(studyManageService.getDetails(id));
    }

    /**
     * 修改播放次数 on 2019/10/24 17:02
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "修改播放次数", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/updatePlayTime")
    public ResponseEntity<?> updatePlayTime(@ApiParam(name = "id", value = "课程id") @RequestParam Long id){
        return ResponseEntity.ok(studyManageService.updatePlayTime(id));
    }

    /**
     * 评论分页 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "评论分页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manage/comments")
    public ResponseEntity<?> comments(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "id", value = "课程id") @RequestParam Long id){
        return ResponseEntity.ok(studyManageService.comments(pn,pageSize,id));
    }

    /**
     * 我的收藏 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "我的收藏", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "collection/mine")
    public ResponseEntity<?> mine(@RequestParam(defaultValue = "1") int pn,
                                  @RequestParam(defaultValue = "30") int pageSize,
                                  @ApiParam(name = "accountId", value = "用户id") @RequestParam Long accountId){
        return ResponseEntity.ok(studyManageService.mine(pn,pageSize,accountId));
    }

    /**
     * 新增收藏 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "新增收藏", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "collection/save")
    public ResponseEntity<?> collectionSave(@RequestParam Long classId){
        return ResponseEntity.ok(studyManageService.collectionSave(classId));
    }

    /**
     * 取消收藏 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "取消收藏", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "collection/cancel")
    public ResponseEntity<?> cancel(@ApiParam(name = "collectionId", value = "收藏id") @RequestParam Long collectionId){
        return ResponseEntity.ok(studyManageService.cancel(collectionId));
    }

    /**
     * 学习记录 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "学习记录", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "history/mine")
    public ResponseEntity<?> historyMine(@RequestParam(defaultValue = "1") int pn,
                                  @RequestParam(defaultValue = "30") int pageSize,
                                  @ApiParam(name = "accountId", value = "用户id") @RequestParam Long accountId){
        return ResponseEntity.ok(studyManageService.historyMine(pn,pageSize,accountId));
    }

    /**
     * 新增学习记录 on 2019/12/24 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "新增学习记录", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping(value = "history/save")
    public ResponseEntity<?> historySave(@RequestParam Long classId){
        return ResponseEntity.ok(studyManageService.historySave(classId));
    }

    /**
     * 管理端课程文件预览 on 2019/12/30 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "管理端课程文件预览", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "manageView")
    public ResponseEntity<?> manageView(@ApiParam(name = "url",value = "文件地址") @RequestParam String url,HttpServletRequest request){
        return studyManageService.view("MANAGE",url,request);
    }

    /**
     * 用户端课程文件预览 on 2019/12/30 15:38
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "用户端课程文件预览", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping(value = "userView")
    public ResponseEntity<?> userView(@ApiParam(name = "url",value = "文件地址") @RequestParam String url,HttpServletRequest request){
        return studyManageService.view("USER",url,request);
    }
}